import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Jdbc04 {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        Statement statement = null;
        try {
            String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=UTF-8&useSSL=true";
            String username = "root";
            String password = "root";
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            connection.setAutoCommit(false);
            statement = connection.createStatement();
            String sql1 = "update account set money = money-100 where name = 'A'";
            String sql2 = "update account set money = money+100 where name = 'B'";
            statement.executeUpdate(sql1);
            statement.executeUpdate(sql2);
            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            statement.close();
            connection.close();
        }

    }
}
